<template>
    <div class="views-shangpinduihuantongji">
        <div>
            <e-container>
                <el-form action="javascript:;" @submit="searchSubmit" inline>
                    <el-form-item label="商品名称">
                        <el-input v-model="search.shangpinmingcheng"></el-input>
                    </el-form-item>
                    <el-form-item label="添加时间">
                        <div style="display: flex">
                            <el-date-picker v-model="search.addtime_start" type="date" value-format="YYYY-MM-DD" placeholder="选择开始日期" />
                            -
                            <el-date-picker v-model="search.addtime_end" type="date" value-format="YYYY-MM-DD" placeholder="选择结束日期" />
                        </div> </el-form-item
                ></el-form>
            </e-container>
        </div>
        <div>
            <e-container>
                <div class="title-modelbox-widget1">
                    <h3 class="section-title">兑换统计</h3>
                    <div class="sidebar-widget-body">
                        <div style="padding: 10px; background: #ffffff">
                            <el-table :data="duihuanList">
                                <el-table-column prop="shangpinmingcheng" label="商品名称" width="180" />
                                <el-table-column prop="count" label="兑换数量" sortable />
                            </el-table>
                        </div>

                        <v3-echarts :options="LineOptions" width="100%" height="300px"></v3-echarts>

                        <v3-echarts :options="barOptions" width="100%" height="300px"></v3-echarts>
                    </div>
                    <!-- /.sidebar-widget-body -->
                </div>
            </e-container>
        </div>
    </div>
</template>

<script setup>
    import http from "@/utils/ajax/http";
    import DB from "@/utils/db";

    import { ref, reactive, watch, computed, unref } from "vue";
    import { useRoute } from "vue-router";
    import { session } from "@/utils/utils";
    import { extend } from "@/utils/extend";

    const route = useRoute();

    const search = reactive({ shangpinmingcheng: "", addtime_start: "", addtime_end: "" });
    extend(search, route.query);
    const serachSubmit = () => {
        loadDatabaseList();
    };
    var timer;
    watch(
        () => search,
        () => {
            clearTimeout(timer);
            timer = setTimeout(() => {
                serachSubmit();
            }, 200);
        },
        { deep: true }
    );
    const duihuanList = ref([]);
    const loadDatabaseList = async () => {
        var where = " 1=1 ";
        if (search.shangpinmingcheng) {
            where += ` AND dh.shangpinmingcheng LIKE '%${search.shangpinmingcheng}%' `;
        }
        if (search.addtime_start) {
            where += ` AND dh.addtime>='${search.addtime_start}' `;
        }
        if (search.addtime_end) {
            where += ` AND dh.addtime<='${search.addtime_end}' `;
        }
        duihuanList.value = await DB.name("duihuan").alias("dh").field("dh.shangpinmingcheng").group("dh.shangpinmingcheng").field("count(*) count").where(where).select();
    };

    loadDatabaseList();
    const LineOptions = computed(() => {
        var list = unref(duihuanList);
        return {
            title: {
                text: "",
            },
            tooltip: {
                trigger: "axis",
            },
            grid: {
                left: "3%",
                right: "4%",
                bottom: "3%",
                containLabel: true,
            },
            toolbox: {
                feature: {
                    saveAsImage: {},
                },
            },
            xAxis: {
                type: "category",
                boundaryGap: false,
                data: list.map((s) => s.shangpinmingcheng),
            },
            yAxis: {
                type: "value",
            },
            series: [
                {
                    name: "兑换数量",
                    type: "line",
                    smooth: true,
                    data: list.map((s) => parseFloat(s.count)),
                },
            ],
        };
    });

    const barOptions = computed(() => {
        var list = unref(duihuanList);
        return {
            title: {
                text: "",
                left: "center",
            },
            tooltip: {
                trigger: "item",
                formatter: "{b}：{c},{d}%",
            },
            legend: {
                orient: "vertical",
                left: "left",
            },
            series: [
                {
                    name: "兑换数量",
                    type: "pie",
                    radius: "50%",
                    data: list.map((s) => {
                        return {
                            value: parseFloat(s.count),
                            name: s.shangpinmingcheng,
                        };
                    }),
                    emphasis: {
                        itemStyle: {
                            shadowBlur: 10,
                            shadowOffsetX: 0,
                            shadowColor: "rgba(0, 0, 0, 0.5)",
                        },
                    },
                },
            ],
        };
    });
</script>

<style scoped lang="scss">
    .views-shangpinduihuantongji {
    }
</style>
